#ifndef __AI_SNAKE_H__
#define __AI_SNAKE_H__

#include <list>
#include "point.h"
#include "bmp.h"
#include "game_map.h"
#include "snake.h"

class AISnake
{
private:
    // 构造函数新增 GameMap* 参数，初始化成员变量 m_map
    AISnake(int *mp, Snake *snake, GameMap *map) : mp(mp), playerSnake(snake), m_map(map),
                                                   body("./res/ai_snack_body.bmp"), head_up("./res/ai_snack_head_up.bmp"),
                                                   head_down("./res/ai_snack_head_down.bmp"), head_left("./res/ai_snack_head_left.bmp"),
                                                   head_right("./res/ai_snack_head_right.bmp"), tail_up("./res/ai_snack_tail_up.bmp"),
                                                   tail_down("./res/ai_snack_tail_down.bmp"), tail_left("./res/ai_snack_tail_left.bmp"),
                                                   tail_right("./res/ai_snack_tail_right.bmp"), direction(RIGHT)
    {
        init(10, 10);
    }

public:
    ~AISnake();

    void init(int x = 10, int y = 10);
    void show();
    bool move(); // true ai蛇还活着 false 蛇死了
    Point calculateNewHead();
    bool isCollision(const Point &newHead);
    void handleMovement(const Point &newHead);
    bool handleFood(GridState foodType);
    void removeTail();
    void clearAISnake();

    void decideDirection();

    static AISnake *instance(int *mp, Snake *snake, GameMap *map);
    static void delInstance();

private:
    std::list<Point> m_snakeBody;
    static AISnake *m_instance;
    int *mp;
    Snake *playerSnake;
    GameMap *m_map;
    BMP body;
    BMP head_up;
    BMP head_down;
    BMP head_left;
    BMP head_right;
    BMP tail_up;
    BMP tail_down;
    BMP tail_left;
    BMP tail_right;
    int direction;
};

#endif